package com.glsc.ngateway.flowable.listener.leave;

import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.flowable.service.FlowableUserService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class LeaveStartHandler implements TaskListener {

    public static LeaveStartHandler leaveStartHandler;
    @Autowired
    private FlowableUserService flowableUserService;

    @PostConstruct
    public void init() {
        leaveStartHandler = this;
        leaveStartHandler.flowableUserService = this.flowableUserService;
    }


    @Override
    public void notify(DelegateTask delegateTask) {

        //delegateTask.setAssignee("老板");
        String userid = delegateTask.getVariable("userid").toString();
        String content = delegateTask.getVariable("content").toString();

        String department = leaveStartHandler.flowableUserService.getDepartment(userid);
        delegateTask.setVariable("teamleader", leaveStartHandler.flowableUserService.getLeader(userid));
        delegateTask.setVariable("manager", leaveStartHandler.flowableUserService.getManager(department));

        JSONObject json = JSONObject.parseObject(content);
        delegateTask.setVariable("days", json.getString("days"));

    }

}